<?php
/**
 * Created by PhpStorm.
 * User: geyouwen
 * Date: 16/9/9
 * Time: 上午12:26
 */

namespace Superadmin\Controller;


class MemberController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
    }

    //会员列表
    public function index()
    {
        $where = array();
        $mobile = I('get.mobile');
        $username = I('get.username');
        if($mobile)
        {
            $where['mobile'] = $mobile;
        }
        if($username)
        {
            $where['username'] = $username;
        }

        $order      = 'addtime desc';
        $list  =  $this->getpage(D('member'), $where, $order, C('page_size')); //$this->admin->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();
        $level = array();
        if($list['list'])
        {
            $level_ids = i_array_column($list['list'], 'level_id');
            $level = M('level')->where(array('id' =>array('in', $level_ids)))->select();
            if($level)  $level = changeArrIndex($level, 'id');
        }
        $this->assign('list',$list['list']);// 赋值数据集
        $this->assign('page',$list['page']);// 赋值分页输出
        $this->assign('level',$level);// 赋值分页输出
        $this->display(); // 输出模板
    }

    //学员风采
    public function album()
    {
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $order      = 'addtime desc';
        $list  =  $this->getpage(D('album'), '', $order, C('page_size')); //$this->admin->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();
        $big_menu = array(
            'title' => '新风采',
            'uri' => U('member/albumadd'),
        );
        $this->assign('big_menu', $big_menu);
        $this->assign('list',$list['list']);// 赋值数据集
        $this->assign('page',$list['page']);// 赋值分页输出
        $this->display(); // 输出模板
    }

    public function albumAdd()
    {
        if(IS_POST)
        {
            if(D('album')->create())
            {
                D('album')->ordid =  D('album')->ordid ? D('album')->ordid : 255;
                D('album')->addtime = time();
                D('album')->add();
                $this->success(L('operation_success'), u('member/album'));
            } else{
                $msg = D('album')->getError();
                $this->error($msg, u('member/album', u('member/albumadd')));
            }
        }
        $this->display(); // 输出模板
    }

    public function albumdel()
    {
        if(IS_AJAX)
        {
            $id = I('get.id');
            $where = array('id' => $id);
            D('album')->where($where)->delete();
            $this->ajaxReturn(array('status'=>1,'msg'=>L('operation_success')));
        }
    }

    public function albumedit()
    {
       if(IS_POST)
       {
           if(D('album')->create())
           {
               D('album')->save();
               $this->success(L('operation_success'), u('member/album'));
           } else {
               $msg = D('album')->getError();
               $this->error($msg, u('member/album', u('member/albumadd')));
           }
       }
        $id = I('id');
        $info = D('album')->find($id);
        $this->assign('info', $info);
        $this->display();
    }


    public function family()
    {
        $uid = I('id');
        $where = array('uid' => $uid);
        $list  = M('member_relation')->where($where)->select();
        $this->assign('list', $list);
        $this->display();
    }

    public function delete()
    {
        if(IS_AJAX)
        {
            $id = I('id');
            $where = array('id' => $id);
            M('member')->where($where)->delete();
            $back = array('status' => 1, 'msg'=> L('operation_success'));
            $this->ajaxReturn($back);
        }
    }

    public function edit()
    {
        if(IS_AJAX)
        {
            if(D('member')->create())
            {
                $id = I('post.id');
                if(I('post.password'))
                {
                    D('member')->password = md5(I('post.password'));
                } else{
                    unset(D('member')->password);
                }
                if(I('post.score', 0, 'intval'))
                {
                    D('member')->level_id = D('level')->getLevelId(I('post.score'));
                }
                D('member')->where(array('id' => $id))->save();
                $back = array('status' => 1, 'msg' => '操作成功');
            } else {
                $msg = D('member')->getError();
                $back = array('status' => 0, 'msg' => $msg);
            }
            $this->ajaxReturn($back);
        }
        $id = I('id');
        $info = M('member')->find($id);
        $this->assign('info', $info);
        $this->display();
    }

    public function add()
    {
        if(IS_AJAX)
        {
            if(D('member')->create())
            {
                D('member')->password = md5(I('post.password'));
                D('member')->addtime = time();
                if(I('post.score', 0, 'intval'))
                {
                    D('member')->level_id = D('level')-> getLevelId(I('post.score'));
                }
                D('member')->add();
                $back = array('status' => 1, 'msg' => '操作成功');
            } else {
                $msg = D('member')->getError();
                $back = array('status' => 0, 'msg' => $msg);
            }
            $this->ajaxReturn($back);
        }
        $this->display();
    }


    //查看更多亲属
    public function morefamily()
    {
        $mobile = I('tel');
        $uid = M('member')->where(array('mobile' => $mobile))->getField('id');
        if($uid) $list = M('member_relation')->where(array('uid' => $uid))->select();
        $this->assign('list', $list);
        $this->display();
    }


}